package com.yuanrui.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yuanrui.subject.application.convert.SubjectLabelDTOConverter;
import com.yuanrui.subject.application.dto.SubjectLabelDTO;
import com.yuanrui.subject.common.entity.Result;
import com.yuanrui.subject.domain.entity.SubjectLabelBo;
import com.yuanrui.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目标签controller
 */
@RestController
@Slf4j
@RequestMapping("/subject/label")
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;
    /**
     * 新增标签
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(), "标签名称不能为空");
            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE.convertToBo(subjectLabelDTO);
            Boolean insert = subjectLabelDomainService.addLabel(subjectLabelBo);
            return Result.ok(insert);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增标签失败");
        }
    }


    /**
     * 修改标签
     */
    @PostMapping("/update")
    public Result<Boolean> updateLabel(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空");
            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE.convertToBo(subjectLabelDTO);
            Boolean row = subjectLabelDomainService.updateLabel(subjectLabelBo);
            return Result.ok(row);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增标签失败");
        }
    }

    /**
     * 删除标签
     */
    @PostMapping("/delete")
    public Result<Boolean> deleteLabel(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空");
            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE.convertToBo(subjectLabelDTO);
            Boolean row = subjectLabelDomainService.deleteLabel(subjectLabelBo);
            return Result.ok(row);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增标签失败");
        }
    }

    /**
     * 查询分类下标签
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空");
            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE.convertToBo(subjectLabelDTO);
            List<SubjectLabelBo> BoList=subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBo);
            List<SubjectLabelDTO> result = SubjectLabelDTOConverter.INSTANCE.toDTOList(BoList);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("查询该分类下标签失败");
        }
    }
}
